package com.huawei.java.util;

import java.io.*;
import java.util.*;

public class DataResults {
    private List<Integer> supply;
    private Integer[][] allocation;
    private List<List<Integer>> demands;

    private String[] costumeNames;
    private String[] siteNames;
    private Map<String, Integer> siteNamesMap;
    private Map<String, Integer> costumeNamesMap;
    private Integer qosConstraint;

    public DataResults(){
        init();
    }

    public List<Integer> getSupply() {
        return supply;
    }

    public Integer[][] getAllocation() {
        return allocation;
    }

    public List<List<Integer>> getDemands() {
        return demands;
    }


    public void init() {
        siteBandWitHandler(FileUtils.readCSV("/data/site_bandwidth.csv"));
        demandsHandler(FileUtils.readCSV("/data/demand.csv"));
        qosConstraint = FileUtils.scanQosConstraint();
        allocationHandler(FileUtils.readCSV("/data/qos.csv"));

        //先创建输出文件夹
        File d = new File("/output");
        File f = new File("/output/solution.txt");
        if (!d.exists()){
            d.mkdir();
        }
        if (!f.exists()) {
            try {
                f.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    // 提供带宽提供(site_name_bandwith)
    private void siteBandWitHandler(List<String[]> input) {
        int rowsNum = input.size();
        supply = new ArrayList<>();
        siteNames = new String[rowsNum - 1];
        siteNamesMap = new HashMap<>();
        for (int i = 1; i < rowsNum; i++) {
            String[] values = input.get(i);
            supply.add(Integer.valueOf(values[1]));
            siteNames[i - 1] = values[0];
            siteNamesMap.put(values[0], i - 1);
        }
    }

    private void demandsHandler(List<String[]> input) {
        int rowsNum = input.size();
        String[] firstLine = input.get(0);
        int columnsNum = firstLine.length;

        costumeNames = new String[columnsNum - 1];
        costumeNamesMap = new HashMap<>();
        for (int i = 1; i < columnsNum; i++) {
            costumeNames[i - 1] = firstLine[i];
            costumeNamesMap.put(costumeNames[i - 1], i - 1);
        }

        demands = new ArrayList<>();
        for (int i = 1; i < rowsNum; ++i) {
            String[] values = input.get(i);
            List<Integer> tempLine = new ArrayList<>();
            for (int j = 1; j < columnsNum; ++j) {
                tempLine.add(Integer.valueOf(values[j]));
            }
            demands.add(tempLine);
        }
    }

    // 处理分配(qos)
    private void allocationHandler(List<String[]> input) {
        int rowsNum = input.size(); //行数

        String[] firstLine = input.get(0);
        int columnNum = firstLine.length; //列数
        Integer[] jMapArray = new Integer[columnNum];
        for (int i = 1; i < columnNum; i++) {
            jMapArray[i] = costumeNamesMap.get(firstLine[i]);
        }

        allocation = new Integer[rowsNum - 1][columnNum - 1]; // 分配矩阵
        for (int i = 1; i < rowsNum; i++) {
            String[] rows = input.get(i); //一行
            Integer iMap = siteNamesMap.get(rows[0]);

            for (int j = 1; j < columnNum; j++) {

                int item = Integer.parseInt(rows[j]);
                item = item < qosConstraint ? Integer.MAX_VALUE : 0;
//                allocation[i-1][j-1] = item;
                allocation[iMap][jMapArray[j]] = item; //此处由于不同文件的顺序可能不一样，此处用map映射到正确的坐标
            }
        }
    }

    //写文件
    public void writeAnswer(Integer[][] plan) {

        try(BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("/output/solution.txt", true)))) {
            for (int i = 0; i < plan.length; i++) {
                StringBuilder line = new StringBuilder();
                line.append(costumeNames[i]).append(":");
                Integer[] row = plan[i];

                int columns = 0;
                for (int j = 0; j < row.length; j++) {
                    if (plan[i][j] > 0) {
                        if (columns > 0) line.append(",");
                        line.append("<").append(siteNames[j]).append(",").append(plan[i][j]).append(">");
                        ++columns;
                    }
                }

                bw.write(line.toString());
                bw.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
